
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 12/15/2011 02:26:07
-- Generated from EDMX file: C:\Users\Bartal\Dropbox\Visual Studio Projects\StatusPlan3\StatusPlan3\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [StatusPlan3];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Contact_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Contact] DROP CONSTRAINT [FK_Contact_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Created_By]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project] DROP CONSTRAINT [FK_Created_By];
GO
IF OBJECT_ID(N'[dbo].[FK_File_File]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[File] DROP CONSTRAINT [FK_File_File];
GO
IF OBJECT_ID(N'[dbo].[FK_File_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[File] DROP CONSTRAINT [FK_File_User];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Attachment_File]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue_Attachment] DROP CONSTRAINT [FK_Issue_Attachment_File];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Attachment_Issue]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue_Attachment] DROP CONSTRAINT [FK_Issue_Attachment_Issue];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Created_By]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Created_By];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Issue_Status]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Issue_Status];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Issue_Type]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Issue_Type];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Message_Issue]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue_Message] DROP CONSTRAINT [FK_Issue_Message_Issue];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Message_Message]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue_Message] DROP CONSTRAINT [FK_Issue_Message_Message];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Priority]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Priority];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Responsible]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Responsible];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Status_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue_Status] DROP CONSTRAINT [FK_Issue_Status_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Subproject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue] DROP CONSTRAINT [FK_Issue_Subproject];
GO
IF OBJECT_ID(N'[dbo].[FK_Issue_Type_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Issue_Type] DROP CONSTRAINT [FK_Issue_Type_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Log_Issue_Issue]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Log_Issue] DROP CONSTRAINT [FK_Log_Issue_Issue];
GO
IF OBJECT_ID(N'[dbo].[FK_Log_Project_Group_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Log_Project_Group] DROP CONSTRAINT [FK_Log_Project_Group_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Log_Project_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Log_Project] DROP CONSTRAINT [FK_Log_Project_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_Log_Subproject_Subproject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Log_Subproject] DROP CONSTRAINT [FK_Log_Subproject_Subproject];
GO
IF OBJECT_ID(N'[dbo].[FK_Log_Task_Task]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Log_Task] DROP CONSTRAINT [FK_Log_Task_Task];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_Attachment_File]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Message_Attachment] DROP CONSTRAINT [FK_Message_Attachment_File];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_Attachment_Message]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Message_Attachment] DROP CONSTRAINT [FK_Message_Attachment_Message];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Message] DROP CONSTRAINT [FK_Message_User];
GO
IF OBJECT_ID(N'[dbo].[FK_org_message]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Message_Reply] DROP CONSTRAINT [FK_org_message];
GO
IF OBJECT_ID(N'[dbo].[FK_Priority_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Priority] DROP CONSTRAINT [FK_Priority_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Attachment_File]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project_Attachment] DROP CONSTRAINT [FK_Project_Attachment_File];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Attachment_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project_Attachment] DROP CONSTRAINT [FK_Project_Attachment_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project] DROP CONSTRAINT [FK_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Group_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project_Group] DROP CONSTRAINT [FK_Project_Group_User];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Member_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project_Member] DROP CONSTRAINT [FK_Project_Member_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Member_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project_Member] DROP CONSTRAINT [FK_Project_Member_User];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Message_Message]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project_Message] DROP CONSTRAINT [FK_Project_Message_Message];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Message_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project_Message] DROP CONSTRAINT [FK_Project_Message_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_Reply_Message]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Message_Reply] DROP CONSTRAINT [FK_Reply_Message];
GO
IF OBJECT_ID(N'[dbo].[FK_Responsible]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project] DROP CONSTRAINT [FK_Responsible];
GO
IF OBJECT_ID(N'[dbo].[FK_Status_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Status] DROP CONSTRAINT [FK_Status_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_Subproject_Attachment_File]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subproject_Attachment] DROP CONSTRAINT [FK_Subproject_Attachment_File];
GO
IF OBJECT_ID(N'[dbo].[FK_Subproject_Attachment_Subproject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subproject_Attachment] DROP CONSTRAINT [FK_Subproject_Attachment_Subproject];
GO
IF OBJECT_ID(N'[dbo].[FK_Subproject_Created_By]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subproject] DROP CONSTRAINT [FK_Subproject_Created_By];
GO
IF OBJECT_ID(N'[dbo].[FK_Subproject_Message_Message]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subproject_Message] DROP CONSTRAINT [FK_Subproject_Message_Message];
GO
IF OBJECT_ID(N'[dbo].[FK_Subproject_Message_Subproject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subproject_Message] DROP CONSTRAINT [FK_Subproject_Message_Subproject];
GO
IF OBJECT_ID(N'[dbo].[FK_Subproject_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subproject] DROP CONSTRAINT [FK_Subproject_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_Subproject_Responsible]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subproject] DROP CONSTRAINT [FK_Subproject_Responsible];
GO
IF OBJECT_ID(N'[dbo].[FK_Taks_Attachment_File]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Taks_Attachment] DROP CONSTRAINT [FK_Taks_Attachment_File];
GO
IF OBJECT_ID(N'[dbo].[FK_Taks_Attachment_Task]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Taks_Attachment] DROP CONSTRAINT [FK_Taks_Attachment_Task];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Created_By]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Created_By];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Issue_Issue]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task_Issue] DROP CONSTRAINT [FK_Task_Issue_Issue];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Issue_Task]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task_Issue] DROP CONSTRAINT [FK_Task_Issue_Task];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Message_Message]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task_Message] DROP CONSTRAINT [FK_Task_Message_Message];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Message_Task]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task_Message] DROP CONSTRAINT [FK_Task_Message_Task];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Priority]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Priority];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Responsible]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Responsible];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Status]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Status];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Subproject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Subproject];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Task]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Task];
GO
IF OBJECT_ID(N'[dbo].[FK_Task_Type]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Task] DROP CONSTRAINT [FK_Task_Type];
GO
IF OBJECT_ID(N'[dbo].[FK_Type_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Type] DROP CONSTRAINT [FK_Type_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Roll_Role]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[User_Roll] DROP CONSTRAINT [FK_User_Roll_Role];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Roll_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[User_Roll] DROP CONSTRAINT [FK_User_Roll_User];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Standard_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[User_Standard] DROP CONSTRAINT [FK_User_Standard_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Standard_Project_Group]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[User_Standard] DROP CONSTRAINT [FK_User_Standard_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Standard_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[User_Standard] DROP CONSTRAINT [FK_User_Standard_User];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Company]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Company];
GO
IF OBJECT_ID(N'[dbo].[Contact]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Contact];
GO
IF OBJECT_ID(N'[dbo].[File]', 'U') IS NOT NULL
    DROP TABLE [dbo].[File];
GO
IF OBJECT_ID(N'[dbo].[Issue]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Issue];
GO
IF OBJECT_ID(N'[dbo].[Issue_Attachment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Issue_Attachment];
GO
IF OBJECT_ID(N'[dbo].[Issue_Message]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Issue_Message];
GO
IF OBJECT_ID(N'[dbo].[Issue_Status]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Issue_Status];
GO
IF OBJECT_ID(N'[dbo].[Issue_Type]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Issue_Type];
GO
IF OBJECT_ID(N'[dbo].[Log_Issue]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Log_Issue];
GO
IF OBJECT_ID(N'[dbo].[Log_Project]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Log_Project];
GO
IF OBJECT_ID(N'[dbo].[Log_Project_Group]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Log_Project_Group];
GO
IF OBJECT_ID(N'[dbo].[Log_Subproject]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Log_Subproject];
GO
IF OBJECT_ID(N'[dbo].[Log_Task]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Log_Task];
GO
IF OBJECT_ID(N'[dbo].[Message]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Message];
GO
IF OBJECT_ID(N'[dbo].[Message_Attachment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Message_Attachment];
GO
IF OBJECT_ID(N'[dbo].[Message_Reply]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Message_Reply];
GO
IF OBJECT_ID(N'[dbo].[Priority]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Priority];
GO
IF OBJECT_ID(N'[dbo].[Project]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Project];
GO
IF OBJECT_ID(N'[dbo].[Project_Attachment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Project_Attachment];
GO
IF OBJECT_ID(N'[dbo].[Project_Group]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Project_Group];
GO
IF OBJECT_ID(N'[dbo].[Project_Member]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Project_Member];
GO
IF OBJECT_ID(N'[dbo].[Project_Message]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Project_Message];
GO
IF OBJECT_ID(N'[dbo].[Role]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Role];
GO
IF OBJECT_ID(N'[dbo].[Status]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Status];
GO
IF OBJECT_ID(N'[dbo].[Subproject]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Subproject];
GO
IF OBJECT_ID(N'[dbo].[Subproject_Attachment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Subproject_Attachment];
GO
IF OBJECT_ID(N'[dbo].[Subproject_Message]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Subproject_Message];
GO
IF OBJECT_ID(N'[dbo].[Taks_Attachment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Taks_Attachment];
GO
IF OBJECT_ID(N'[dbo].[Task]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Task];
GO
IF OBJECT_ID(N'[dbo].[Task_Issue]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Task_Issue];
GO
IF OBJECT_ID(N'[dbo].[Task_Message]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Task_Message];
GO
IF OBJECT_ID(N'[dbo].[Type]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Type];
GO
IF OBJECT_ID(N'[dbo].[User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User];
GO
IF OBJECT_ID(N'[dbo].[User_Roll]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User_Roll];
GO
IF OBJECT_ID(N'[dbo].[User_Standard]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User_Standard];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Companies'
CREATE TABLE [dbo].[Companies] (
    [id] int IDENTITY(1,1) NOT NULL,
    [company_name] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'Contacts'
CREATE TABLE [dbo].[Contacts] (
    [id] int IDENTITY(1,1) NOT NULL,
    [first_name] nvarchar(50)  NOT NULL,
    [last_name] nvarchar(50)  NULL,
    [phone] nvarchar(50)  NULL,
    [mobile] nvarchar(50)  NULL,
    [email] nvarchar(50)  NULL,
    [company_id] int  NOT NULL
);
GO

-- Creating table 'Files'
CREATE TABLE [dbo].[Files] (
    [id] int IDENTITY(1,1) NOT NULL,
    [file_content] varbinary(max)  NULL,
    [file_name] nvarchar(250)  NOT NULL,
    [file_type] nvarchar(50)  NOT NULL,
    [date_added] datetime  NOT NULL,
    [added_by] int  NOT NULL,
    [is_folder] int  NULL,
    [folder] int  NULL,
    [size] decimal(18,0)  NOT NULL
);
GO

-- Creating table 'Issues'
CREATE TABLE [dbo].[Issues] (
    [id] int IDENTITY(1,1) NOT NULL,
    [project_id] int  NOT NULL,
    [subproject_id] int  NULL,
    [created_by] int  NOT NULL,
    [created_date] datetime  NOT NULL,
    [responsible] int  NULL,
    [name] nvarchar(200)  NOT NULL,
    [description] nvarchar(max)  NULL,
    [start_date] datetime  NULL,
    [end_date] datetime  NULL,
    [planed_end_date] datetime  NULL,
    [deadline] datetime  NULL,
    [estimated_hours] decimal(18,0)  NULL,
    [used_hours] decimal(18,0)  NULL,
    [progress] decimal(18,0)  NULL,
    [added_estimated_hours] decimal(18,0)  NULL,
    [company_id] int  NULL,
    [issue_type_id] int  NULL,
    [issue_status_id] int  NULL,
    [priority_id] int  NULL
);
GO

-- Creating table 'Issue_Attachment'
CREATE TABLE [dbo].[Issue_Attachment] (
    [id] int IDENTITY(1,1) NOT NULL,
    [issue_id] int  NOT NULL,
    [file_id] int  NOT NULL
);
GO

-- Creating table 'Issue_Message'
CREATE TABLE [dbo].[Issue_Message] (
    [id] int IDENTITY(1,1) NOT NULL,
    [issue_id] int  NOT NULL,
    [message_id] int  NOT NULL
);
GO

-- Creating table 'Issue_Status'
CREATE TABLE [dbo].[Issue_Status] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [project_group_id] int  NOT NULL,
    [can_edit] int  NOT NULL
);
GO

-- Creating table 'Issue_Type'
CREATE TABLE [dbo].[Issue_Type] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [project_group_id] int  NOT NULL
);
GO

-- Creating table 'Log_Issue'
CREATE TABLE [dbo].[Log_Issue] (
    [id] int  NOT NULL,
    [body] nvarchar(max)  NOT NULL,
    [issue_id] int  NOT NULL,
    [created_date] datetime  NOT NULL
);
GO

-- Creating table 'Log_Project'
CREATE TABLE [dbo].[Log_Project] (
    [id] int IDENTITY(1,1) NOT NULL,
    [body] nvarchar(max)  NOT NULL,
    [created_date] datetime  NOT NULL,
    [project_id] int  NOT NULL
);
GO

-- Creating table 'Log_Project_Group'
CREATE TABLE [dbo].[Log_Project_Group] (
    [id] int IDENTITY(1,1) NOT NULL,
    [body] nvarchar(max)  NOT NULL,
    [created_date] datetime  NOT NULL,
    [project_group_id] int  NOT NULL
);
GO

-- Creating table 'Log_Subproject'
CREATE TABLE [dbo].[Log_Subproject] (
    [id] int IDENTITY(1,1) NOT NULL,
    [body] nvarchar(max)  NOT NULL,
    [created_date] datetime  NOT NULL,
    [subproject_id] int  NOT NULL
);
GO

-- Creating table 'Log_Task'
CREATE TABLE [dbo].[Log_Task] (
    [id] int IDENTITY(1,1) NOT NULL,
    [body] nvarchar(max)  NOT NULL,
    [created_date] datetime  NOT NULL,
    [task_id] int  NOT NULL
);
GO

-- Creating table 'Messages'
CREATE TABLE [dbo].[Messages] (
    [id] int IDENTITY(1,1) NOT NULL,
    [subject] varchar(250)  NOT NULL,
    [message1] varchar(max)  NOT NULL,
    [created_date] datetime  NOT NULL,
    [sender] int  NOT NULL
);
GO

-- Creating table 'Message_Attachment'
CREATE TABLE [dbo].[Message_Attachment] (
    [id] int IDENTITY(1,1) NOT NULL,
    [message_id] int  NOT NULL,
    [file_id] int  NOT NULL
);
GO

-- Creating table 'Message_Reply'
CREATE TABLE [dbo].[Message_Reply] (
    [id] int IDENTITY(1,1) NOT NULL,
    [org_message_id] int  NOT NULL,
    [reply_message_id] int  NOT NULL
);
GO

-- Creating table 'Priorities'
CREATE TABLE [dbo].[Priorities] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [priority_index] int  NOT NULL,
    [project_group_id] int  NOT NULL
);
GO

-- Creating table 'Projects'
CREATE TABLE [dbo].[Projects] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [start_date] datetime  NULL,
    [end_date] datetime  NULL,
    [estimated_time] decimal(18,0)  NULL,
    [project_group_id] int  NOT NULL,
    [created_by] int  NOT NULL,
    [created_date] datetime  NOT NULL,
    [responsible] int  NULL,
    [deadline] datetime  NULL
);
GO

-- Creating table 'Project_Attachment'
CREATE TABLE [dbo].[Project_Attachment] (
    [id] int IDENTITY(1,1) NOT NULL,
    [project_id] int  NOT NULL,
    [file_id] int  NOT NULL
);
GO

-- Creating table 'Project_Group'
CREATE TABLE [dbo].[Project_Group] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [start_date] datetime  NULL,
    [end_date] datetime  NULL,
    [planned_end_date] datetime  NULL,
    [active] int  NOT NULL,
    [created_by] int  NOT NULL,
    [created_date] datetime  NOT NULL
);
GO

-- Creating table 'Project_Member'
CREATE TABLE [dbo].[Project_Member] (
    [id] int  NOT NULL,
    [user_id] int  NOT NULL,
    [project_group_id] int  NOT NULL
);
GO

-- Creating table 'Project_Message'
CREATE TABLE [dbo].[Project_Message] (
    [id] int IDENTITY(1,1) NOT NULL,
    [project_id] int  NOT NULL,
    [message_id] int  NOT NULL
);
GO

-- Creating table 'Roles'
CREATE TABLE [dbo].[Roles] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'Status'
CREATE TABLE [dbo].[Status] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [project_hroup_id] int  NOT NULL,
    [is_editable] int  NOT NULL
);
GO

-- Creating table 'Subprojects'
CREATE TABLE [dbo].[Subprojects] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [start_date] datetime  NULL,
    [planned_end_date] datetime  NULL,
    [end_date] datetime  NULL,
    [estimated_time] decimal(18,0)  NULL,
    [created_by] int  NOT NULL,
    [created_date] datetime  NOT NULL,
    [project_id] int  NOT NULL,
    [responsible] int  NULL
);
GO

-- Creating table 'Subproject_Attachment'
CREATE TABLE [dbo].[Subproject_Attachment] (
    [id] int IDENTITY(1,1) NOT NULL,
    [subproject_id] int  NOT NULL,
    [file_id] int  NOT NULL
);
GO

-- Creating table 'Subproject_Message'
CREATE TABLE [dbo].[Subproject_Message] (
    [id] int IDENTITY(1,1) NOT NULL,
    [subproject_id] int  NOT NULL,
    [message_id] int  NOT NULL
);
GO

-- Creating table 'Taks_Attachment'
CREATE TABLE [dbo].[Taks_Attachment] (
    [id] int IDENTITY(1,1) NOT NULL,
    [task_id] int  NOT NULL,
    [file_id] int  NOT NULL
);
GO

-- Creating table 'Tasks'
CREATE TABLE [dbo].[Tasks] (
    [id] int IDENTITY(1,1) NOT NULL,
    [project_id] int  NOT NULL,
    [subproject_id] int  NULL,
    [task_priority_id] int  NULL,
    [task_type_id] int  NULL,
    [task_status_id] int  NULL,
    [created_by] int  NOT NULL,
    [created_date] datetime  NOT NULL,
    [responsible] int  NULL,
    [name] nvarchar(50)  NOT NULL,
    [description] nvarchar(max)  NULL,
    [started_date] datetime  NULL,
    [planned_end_date] datetime  NULL,
    [end_date] datetime  NULL,
    [estimated_time] decimal(18,0)  NULL,
    [time_used] decimal(18,0)  NULL,
    [deadline] datetime  NULL,
    [progress] decimal(18,0)  NULL,
    [added_estimated_hours] decimal(18,0)  NULL,
    [parent_task] int  NULL
);
GO

-- Creating table 'Task_Issue'
CREATE TABLE [dbo].[Task_Issue] (
    [id] int IDENTITY(1,1) NOT NULL,
    [task_id] int  NOT NULL,
    [issues_id] int  NOT NULL
);
GO

-- Creating table 'Task_Message'
CREATE TABLE [dbo].[Task_Message] (
    [id] int IDENTITY(1,1) NOT NULL,
    [message_id] int  NOT NULL,
    [task_id] int  NOT NULL
);
GO

-- Creating table 'Types'
CREATE TABLE [dbo].[Types] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [project_group_id] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [id] int IDENTITY(1,1) NOT NULL,
    [user_name] nvarchar(50)  NOT NULL,
    [password] nvarchar(50)  NOT NULL,
    [change_password] int  NOT NULL,
    [last_changed_password] datetime  NULL,
    [windows_login] nvarchar(50)  NULL,
    [first_name] nvarchar(50)  NOT NULL,
    [last_name] nvarchar(50)  NULL,
    [email] nvarchar(50)  NULL
);
GO

-- Creating table 'User_Roll'
CREATE TABLE [dbo].[User_Roll] (
    [id] int IDENTITY(1,1) NOT NULL,
    [user_id] int  NOT NULL,
    [role_id] int  NOT NULL
);
GO

-- Creating table 'User_Standard'
CREATE TABLE [dbo].[User_Standard] (
    [id] int IDENTITY(1,1) NOT NULL,
    [user_id] int  NOT NULL,
    [project_group_id] int  NULL,
    [project_id] int  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [id] in table 'Companies'
ALTER TABLE [dbo].[Companies]
ADD CONSTRAINT [PK_Companies]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Contacts'
ALTER TABLE [dbo].[Contacts]
ADD CONSTRAINT [PK_Contacts]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Files'
ALTER TABLE [dbo].[Files]
ADD CONSTRAINT [PK_Files]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [PK_Issues]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Issue_Attachment'
ALTER TABLE [dbo].[Issue_Attachment]
ADD CONSTRAINT [PK_Issue_Attachment]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Issue_Message'
ALTER TABLE [dbo].[Issue_Message]
ADD CONSTRAINT [PK_Issue_Message]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Issue_Status'
ALTER TABLE [dbo].[Issue_Status]
ADD CONSTRAINT [PK_Issue_Status]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Issue_Type'
ALTER TABLE [dbo].[Issue_Type]
ADD CONSTRAINT [PK_Issue_Type]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Log_Issue'
ALTER TABLE [dbo].[Log_Issue]
ADD CONSTRAINT [PK_Log_Issue]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Log_Project'
ALTER TABLE [dbo].[Log_Project]
ADD CONSTRAINT [PK_Log_Project]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Log_Project_Group'
ALTER TABLE [dbo].[Log_Project_Group]
ADD CONSTRAINT [PK_Log_Project_Group]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Log_Subproject'
ALTER TABLE [dbo].[Log_Subproject]
ADD CONSTRAINT [PK_Log_Subproject]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Log_Task'
ALTER TABLE [dbo].[Log_Task]
ADD CONSTRAINT [PK_Log_Task]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [PK_Messages]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Message_Attachment'
ALTER TABLE [dbo].[Message_Attachment]
ADD CONSTRAINT [PK_Message_Attachment]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Message_Reply'
ALTER TABLE [dbo].[Message_Reply]
ADD CONSTRAINT [PK_Message_Reply]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Priorities'
ALTER TABLE [dbo].[Priorities]
ADD CONSTRAINT [PK_Priorities]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [PK_Projects]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Project_Attachment'
ALTER TABLE [dbo].[Project_Attachment]
ADD CONSTRAINT [PK_Project_Attachment]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Project_Group'
ALTER TABLE [dbo].[Project_Group]
ADD CONSTRAINT [PK_Project_Group]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Project_Member'
ALTER TABLE [dbo].[Project_Member]
ADD CONSTRAINT [PK_Project_Member]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Project_Message'
ALTER TABLE [dbo].[Project_Message]
ADD CONSTRAINT [PK_Project_Message]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [PK_Roles]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Status'
ALTER TABLE [dbo].[Status]
ADD CONSTRAINT [PK_Status]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Subprojects'
ALTER TABLE [dbo].[Subprojects]
ADD CONSTRAINT [PK_Subprojects]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Subproject_Attachment'
ALTER TABLE [dbo].[Subproject_Attachment]
ADD CONSTRAINT [PK_Subproject_Attachment]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Subproject_Message'
ALTER TABLE [dbo].[Subproject_Message]
ADD CONSTRAINT [PK_Subproject_Message]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Taks_Attachment'
ALTER TABLE [dbo].[Taks_Attachment]
ADD CONSTRAINT [PK_Taks_Attachment]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [PK_Tasks]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Task_Issue'
ALTER TABLE [dbo].[Task_Issue]
ADD CONSTRAINT [PK_Task_Issue]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Task_Message'
ALTER TABLE [dbo].[Task_Message]
ADD CONSTRAINT [PK_Task_Message]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Types'
ALTER TABLE [dbo].[Types]
ADD CONSTRAINT [PK_Types]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'User_Roll'
ALTER TABLE [dbo].[User_Roll]
ADD CONSTRAINT [PK_User_Roll]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'User_Standard'
ALTER TABLE [dbo].[User_Standard]
ADD CONSTRAINT [PK_User_Standard]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [company_id] in table 'Contacts'
ALTER TABLE [dbo].[Contacts]
ADD CONSTRAINT [FK_Contact_Company]
    FOREIGN KEY ([company_id])
    REFERENCES [dbo].[Companies]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Contact_Company'
CREATE INDEX [IX_FK_Contact_Company]
ON [dbo].[Contacts]
    ([company_id]);
GO

-- Creating foreign key on [company_id] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Company]
    FOREIGN KEY ([company_id])
    REFERENCES [dbo].[Companies]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Company'
CREATE INDEX [IX_FK_Issue_Company]
ON [dbo].[Issues]
    ([company_id]);
GO

-- Creating foreign key on [folder] in table 'Files'
ALTER TABLE [dbo].[Files]
ADD CONSTRAINT [FK_File_File]
    FOREIGN KEY ([folder])
    REFERENCES [dbo].[Files]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_File_File'
CREATE INDEX [IX_FK_File_File]
ON [dbo].[Files]
    ([folder]);
GO

-- Creating foreign key on [added_by] in table 'Files'
ALTER TABLE [dbo].[Files]
ADD CONSTRAINT [FK_File_User]
    FOREIGN KEY ([added_by])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_File_User'
CREATE INDEX [IX_FK_File_User]
ON [dbo].[Files]
    ([added_by]);
GO

-- Creating foreign key on [file_id] in table 'Issue_Attachment'
ALTER TABLE [dbo].[Issue_Attachment]
ADD CONSTRAINT [FK_Issue_Attachment_File]
    FOREIGN KEY ([file_id])
    REFERENCES [dbo].[Files]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Attachment_File'
CREATE INDEX [IX_FK_Issue_Attachment_File]
ON [dbo].[Issue_Attachment]
    ([file_id]);
GO

-- Creating foreign key on [file_id] in table 'Message_Attachment'
ALTER TABLE [dbo].[Message_Attachment]
ADD CONSTRAINT [FK_Message_Attachment_File]
    FOREIGN KEY ([file_id])
    REFERENCES [dbo].[Files]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_Attachment_File'
CREATE INDEX [IX_FK_Message_Attachment_File]
ON [dbo].[Message_Attachment]
    ([file_id]);
GO

-- Creating foreign key on [file_id] in table 'Project_Attachment'
ALTER TABLE [dbo].[Project_Attachment]
ADD CONSTRAINT [FK_Project_Attachment_File]
    FOREIGN KEY ([file_id])
    REFERENCES [dbo].[Files]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Attachment_File'
CREATE INDEX [IX_FK_Project_Attachment_File]
ON [dbo].[Project_Attachment]
    ([file_id]);
GO

-- Creating foreign key on [file_id] in table 'Subproject_Attachment'
ALTER TABLE [dbo].[Subproject_Attachment]
ADD CONSTRAINT [FK_Subproject_Attachment_File]
    FOREIGN KEY ([file_id])
    REFERENCES [dbo].[Files]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Attachment_File'
CREATE INDEX [IX_FK_Subproject_Attachment_File]
ON [dbo].[Subproject_Attachment]
    ([file_id]);
GO

-- Creating foreign key on [file_id] in table 'Taks_Attachment'
ALTER TABLE [dbo].[Taks_Attachment]
ADD CONSTRAINT [FK_Taks_Attachment_File]
    FOREIGN KEY ([file_id])
    REFERENCES [dbo].[Files]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Taks_Attachment_File'
CREATE INDEX [IX_FK_Taks_Attachment_File]
ON [dbo].[Taks_Attachment]
    ([file_id]);
GO

-- Creating foreign key on [issue_id] in table 'Issue_Attachment'
ALTER TABLE [dbo].[Issue_Attachment]
ADD CONSTRAINT [FK_Issue_Attachment_Issue]
    FOREIGN KEY ([issue_id])
    REFERENCES [dbo].[Issues]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Attachment_Issue'
CREATE INDEX [IX_FK_Issue_Attachment_Issue]
ON [dbo].[Issue_Attachment]
    ([issue_id]);
GO

-- Creating foreign key on [issue_status_id] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Issue_Status]
    FOREIGN KEY ([issue_status_id])
    REFERENCES [dbo].[Issue_Status]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Issue_Status'
CREATE INDEX [IX_FK_Issue_Issue_Status]
ON [dbo].[Issues]
    ([issue_status_id]);
GO

-- Creating foreign key on [issue_type_id] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Issue_Type]
    FOREIGN KEY ([issue_type_id])
    REFERENCES [dbo].[Issue_Type]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Issue_Type'
CREATE INDEX [IX_FK_Issue_Issue_Type]
ON [dbo].[Issues]
    ([issue_type_id]);
GO

-- Creating foreign key on [issue_id] in table 'Issue_Message'
ALTER TABLE [dbo].[Issue_Message]
ADD CONSTRAINT [FK_Issue_Message_Issue]
    FOREIGN KEY ([issue_id])
    REFERENCES [dbo].[Issues]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Message_Issue'
CREATE INDEX [IX_FK_Issue_Message_Issue]
ON [dbo].[Issue_Message]
    ([issue_id]);
GO

-- Creating foreign key on [project_id] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Project]
    FOREIGN KEY ([project_id])
    REFERENCES [dbo].[Projects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Project'
CREATE INDEX [IX_FK_Issue_Project]
ON [dbo].[Issues]
    ([project_id]);
GO

-- Creating foreign key on [created_by] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Project_Member]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Project_Member]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Project_Member'
CREATE INDEX [IX_FK_Issue_Project_Member]
ON [dbo].[Issues]
    ([created_by]);
GO

-- Creating foreign key on [responsible] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Project_Member1]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Project_Member]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Project_Member1'
CREATE INDEX [IX_FK_Issue_Project_Member1]
ON [dbo].[Issues]
    ([responsible]);
GO

-- Creating foreign key on [subproject_id] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Subproject]
    FOREIGN KEY ([subproject_id])
    REFERENCES [dbo].[Subprojects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Subproject'
CREATE INDEX [IX_FK_Issue_Subproject]
ON [dbo].[Issues]
    ([subproject_id]);
GO

-- Creating foreign key on [issue_id] in table 'Log_Issue'
ALTER TABLE [dbo].[Log_Issue]
ADD CONSTRAINT [FK_Log_Issue_Issue]
    FOREIGN KEY ([issue_id])
    REFERENCES [dbo].[Issues]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Log_Issue_Issue'
CREATE INDEX [IX_FK_Log_Issue_Issue]
ON [dbo].[Log_Issue]
    ([issue_id]);
GO

-- Creating foreign key on [issues_id] in table 'Task_Issue'
ALTER TABLE [dbo].[Task_Issue]
ADD CONSTRAINT [FK_Task_Issue_Issue]
    FOREIGN KEY ([issues_id])
    REFERENCES [dbo].[Issues]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Issue_Issue'
CREATE INDEX [IX_FK_Task_Issue_Issue]
ON [dbo].[Task_Issue]
    ([issues_id]);
GO

-- Creating foreign key on [message_id] in table 'Issue_Message'
ALTER TABLE [dbo].[Issue_Message]
ADD CONSTRAINT [FK_Issue_Message_Message]
    FOREIGN KEY ([message_id])
    REFERENCES [dbo].[Messages]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Message_Message'
CREATE INDEX [IX_FK_Issue_Message_Message]
ON [dbo].[Issue_Message]
    ([message_id]);
GO

-- Creating foreign key on [project_group_id] in table 'Issue_Status'
ALTER TABLE [dbo].[Issue_Status]
ADD CONSTRAINT [FK_Issue_Status_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Status_Project_Group'
CREATE INDEX [IX_FK_Issue_Status_Project_Group]
ON [dbo].[Issue_Status]
    ([project_group_id]);
GO

-- Creating foreign key on [project_group_id] in table 'Issue_Type'
ALTER TABLE [dbo].[Issue_Type]
ADD CONSTRAINT [FK_Issue_Type_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Type_Project_Group'
CREATE INDEX [IX_FK_Issue_Type_Project_Group]
ON [dbo].[Issue_Type]
    ([project_group_id]);
GO

-- Creating foreign key on [project_id] in table 'Log_Project'
ALTER TABLE [dbo].[Log_Project]
ADD CONSTRAINT [FK_Log_Project_Project]
    FOREIGN KEY ([project_id])
    REFERENCES [dbo].[Projects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Log_Project_Project'
CREATE INDEX [IX_FK_Log_Project_Project]
ON [dbo].[Log_Project]
    ([project_id]);
GO

-- Creating foreign key on [project_group_id] in table 'Log_Project_Group'
ALTER TABLE [dbo].[Log_Project_Group]
ADD CONSTRAINT [FK_Log_Project_Group_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Log_Project_Group_Project_Group'
CREATE INDEX [IX_FK_Log_Project_Group_Project_Group]
ON [dbo].[Log_Project_Group]
    ([project_group_id]);
GO

-- Creating foreign key on [subproject_id] in table 'Log_Subproject'
ALTER TABLE [dbo].[Log_Subproject]
ADD CONSTRAINT [FK_Log_Subproject_Subproject]
    FOREIGN KEY ([subproject_id])
    REFERENCES [dbo].[Subprojects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Log_Subproject_Subproject'
CREATE INDEX [IX_FK_Log_Subproject_Subproject]
ON [dbo].[Log_Subproject]
    ([subproject_id]);
GO

-- Creating foreign key on [task_id] in table 'Log_Task'
ALTER TABLE [dbo].[Log_Task]
ADD CONSTRAINT [FK_Log_Task_Task]
    FOREIGN KEY ([task_id])
    REFERENCES [dbo].[Tasks]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Log_Task_Task'
CREATE INDEX [IX_FK_Log_Task_Task]
ON [dbo].[Log_Task]
    ([task_id]);
GO

-- Creating foreign key on [message_id] in table 'Message_Attachment'
ALTER TABLE [dbo].[Message_Attachment]
ADD CONSTRAINT [FK_Message_Attachment_Message]
    FOREIGN KEY ([message_id])
    REFERENCES [dbo].[Messages]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_Attachment_Message'
CREATE INDEX [IX_FK_Message_Attachment_Message]
ON [dbo].[Message_Attachment]
    ([message_id]);
GO

-- Creating foreign key on [sender] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Message_User]
    FOREIGN KEY ([sender])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_User'
CREATE INDEX [IX_FK_Message_User]
ON [dbo].[Messages]
    ([sender]);
GO

-- Creating foreign key on [org_message_id] in table 'Message_Reply'
ALTER TABLE [dbo].[Message_Reply]
ADD CONSTRAINT [FK_org_message]
    FOREIGN KEY ([org_message_id])
    REFERENCES [dbo].[Messages]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_org_message'
CREATE INDEX [IX_FK_org_message]
ON [dbo].[Message_Reply]
    ([org_message_id]);
GO

-- Creating foreign key on [message_id] in table 'Project_Message'
ALTER TABLE [dbo].[Project_Message]
ADD CONSTRAINT [FK_Project_Message_Message]
    FOREIGN KEY ([message_id])
    REFERENCES [dbo].[Messages]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Message_Message'
CREATE INDEX [IX_FK_Project_Message_Message]
ON [dbo].[Project_Message]
    ([message_id]);
GO

-- Creating foreign key on [reply_message_id] in table 'Message_Reply'
ALTER TABLE [dbo].[Message_Reply]
ADD CONSTRAINT [FK_Reply_Message]
    FOREIGN KEY ([reply_message_id])
    REFERENCES [dbo].[Messages]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Reply_Message'
CREATE INDEX [IX_FK_Reply_Message]
ON [dbo].[Message_Reply]
    ([reply_message_id]);
GO

-- Creating foreign key on [message_id] in table 'Subproject_Message'
ALTER TABLE [dbo].[Subproject_Message]
ADD CONSTRAINT [FK_Subproject_Message_Message]
    FOREIGN KEY ([message_id])
    REFERENCES [dbo].[Messages]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Message_Message'
CREATE INDEX [IX_FK_Subproject_Message_Message]
ON [dbo].[Subproject_Message]
    ([message_id]);
GO

-- Creating foreign key on [message_id] in table 'Task_Message'
ALTER TABLE [dbo].[Task_Message]
ADD CONSTRAINT [FK_Task_Message_Message]
    FOREIGN KEY ([message_id])
    REFERENCES [dbo].[Messages]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Message_Message'
CREATE INDEX [IX_FK_Task_Message_Message]
ON [dbo].[Task_Message]
    ([message_id]);
GO

-- Creating foreign key on [project_group_id] in table 'Priorities'
ALTER TABLE [dbo].[Priorities]
ADD CONSTRAINT [FK_Priority_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Priority_Project_Group'
CREATE INDEX [IX_FK_Priority_Project_Group]
ON [dbo].[Priorities]
    ([project_group_id]);
GO

-- Creating foreign key on [task_priority_id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Priority]
    FOREIGN KEY ([task_priority_id])
    REFERENCES [dbo].[Priorities]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Priority'
CREATE INDEX [IX_FK_Task_Priority]
ON [dbo].[Tasks]
    ([task_priority_id]);
GO

-- Creating foreign key on [created_by] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_Created_By]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Project_Member]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Created_By'
CREATE INDEX [IX_FK_Created_By]
ON [dbo].[Projects]
    ([created_by]);
GO

-- Creating foreign key on [project_id] in table 'Project_Attachment'
ALTER TABLE [dbo].[Project_Attachment]
ADD CONSTRAINT [FK_Project_Attachment_Project_Group]
    FOREIGN KEY ([project_id])
    REFERENCES [dbo].[Projects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Attachment_Project_Group'
CREATE INDEX [IX_FK_Project_Attachment_Project_Group]
ON [dbo].[Project_Attachment]
    ([project_id]);
GO

-- Creating foreign key on [project_group_id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Group'
CREATE INDEX [IX_FK_Project_Group]
ON [dbo].[Projects]
    ([project_group_id]);
GO

-- Creating foreign key on [project_id] in table 'Project_Message'
ALTER TABLE [dbo].[Project_Message]
ADD CONSTRAINT [FK_Project_Message_Project]
    FOREIGN KEY ([project_id])
    REFERENCES [dbo].[Projects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Message_Project'
CREATE INDEX [IX_FK_Project_Message_Project]
ON [dbo].[Project_Message]
    ([project_id]);
GO

-- Creating foreign key on [responsible] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_Responsible]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Project_Member]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Responsible'
CREATE INDEX [IX_FK_Responsible]
ON [dbo].[Projects]
    ([responsible]);
GO

-- Creating foreign key on [project_id] in table 'Subprojects'
ALTER TABLE [dbo].[Subprojects]
ADD CONSTRAINT [FK_Subproject_Project]
    FOREIGN KEY ([project_id])
    REFERENCES [dbo].[Projects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Project'
CREATE INDEX [IX_FK_Subproject_Project]
ON [dbo].[Subprojects]
    ([project_id]);
GO

-- Creating foreign key on [project_id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Project]
    FOREIGN KEY ([project_id])
    REFERENCES [dbo].[Projects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Project'
CREATE INDEX [IX_FK_Task_Project]
ON [dbo].[Tasks]
    ([project_id]);
GO

-- Creating foreign key on [created_by] in table 'Project_Group'
ALTER TABLE [dbo].[Project_Group]
ADD CONSTRAINT [FK_Project_Group_User]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Group_User'
CREATE INDEX [IX_FK_Project_Group_User]
ON [dbo].[Project_Group]
    ([created_by]);
GO

-- Creating foreign key on [project_group_id] in table 'Project_Member'
ALTER TABLE [dbo].[Project_Member]
ADD CONSTRAINT [FK_Project_Member_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Member_Project_Group'
CREATE INDEX [IX_FK_Project_Member_Project_Group]
ON [dbo].[Project_Member]
    ([project_group_id]);
GO

-- Creating foreign key on [project_hroup_id] in table 'Status'
ALTER TABLE [dbo].[Status]
ADD CONSTRAINT [FK_Status_Project_Group]
    FOREIGN KEY ([project_hroup_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Status_Project_Group'
CREATE INDEX [IX_FK_Status_Project_Group]
ON [dbo].[Status]
    ([project_hroup_id]);
GO

-- Creating foreign key on [project_group_id] in table 'Types'
ALTER TABLE [dbo].[Types]
ADD CONSTRAINT [FK_Type_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Type_Project_Group'
CREATE INDEX [IX_FK_Type_Project_Group]
ON [dbo].[Types]
    ([project_group_id]);
GO

-- Creating foreign key on [user_id] in table 'Project_Member'
ALTER TABLE [dbo].[Project_Member]
ADD CONSTRAINT [FK_Project_Member_User]
    FOREIGN KEY ([user_id])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Member_User'
CREATE INDEX [IX_FK_Project_Member_User]
ON [dbo].[Project_Member]
    ([user_id]);
GO

-- Creating foreign key on [created_by] in table 'Subprojects'
ALTER TABLE [dbo].[Subprojects]
ADD CONSTRAINT [FK_Subproject_Project_Member]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Project_Member]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Project_Member'
CREATE INDEX [IX_FK_Subproject_Project_Member]
ON [dbo].[Subprojects]
    ([created_by]);
GO

-- Creating foreign key on [responsible] in table 'Subprojects'
ALTER TABLE [dbo].[Subprojects]
ADD CONSTRAINT [FK_Subproject_Project_Member1]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Project_Member]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Project_Member1'
CREATE INDEX [IX_FK_Subproject_Project_Member1]
ON [dbo].[Subprojects]
    ([responsible]);
GO

-- Creating foreign key on [responsible] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Responsible]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Project_Member]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Responsible'
CREATE INDEX [IX_FK_Task_Responsible]
ON [dbo].[Tasks]
    ([responsible]);
GO

-- Creating foreign key on [role_id] in table 'User_Roll'
ALTER TABLE [dbo].[User_Roll]
ADD CONSTRAINT [FK_User_Roll_Role]
    FOREIGN KEY ([role_id])
    REFERENCES [dbo].[Roles]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Roll_Role'
CREATE INDEX [IX_FK_User_Roll_Role]
ON [dbo].[User_Roll]
    ([role_id]);
GO

-- Creating foreign key on [task_status_id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Status]
    FOREIGN KEY ([task_status_id])
    REFERENCES [dbo].[Status]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Status'
CREATE INDEX [IX_FK_Task_Status]
ON [dbo].[Tasks]
    ([task_status_id]);
GO

-- Creating foreign key on [subproject_id] in table 'Subproject_Attachment'
ALTER TABLE [dbo].[Subproject_Attachment]
ADD CONSTRAINT [FK_Subproject_Attachment_Subproject]
    FOREIGN KEY ([subproject_id])
    REFERENCES [dbo].[Subprojects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Attachment_Subproject'
CREATE INDEX [IX_FK_Subproject_Attachment_Subproject]
ON [dbo].[Subproject_Attachment]
    ([subproject_id]);
GO

-- Creating foreign key on [subproject_id] in table 'Subproject_Message'
ALTER TABLE [dbo].[Subproject_Message]
ADD CONSTRAINT [FK_Subproject_Message_Subproject]
    FOREIGN KEY ([subproject_id])
    REFERENCES [dbo].[Subprojects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Message_Subproject'
CREATE INDEX [IX_FK_Subproject_Message_Subproject]
ON [dbo].[Subproject_Message]
    ([subproject_id]);
GO

-- Creating foreign key on [subproject_id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Subproject]
    FOREIGN KEY ([subproject_id])
    REFERENCES [dbo].[Subprojects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Subproject'
CREATE INDEX [IX_FK_Task_Subproject]
ON [dbo].[Tasks]
    ([subproject_id]);
GO

-- Creating foreign key on [task_id] in table 'Taks_Attachment'
ALTER TABLE [dbo].[Taks_Attachment]
ADD CONSTRAINT [FK_Taks_Attachment_Task]
    FOREIGN KEY ([task_id])
    REFERENCES [dbo].[Tasks]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Taks_Attachment_Task'
CREATE INDEX [IX_FK_Taks_Attachment_Task]
ON [dbo].[Taks_Attachment]
    ([task_id]);
GO

-- Creating foreign key on [created_by] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Created_By]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Created_By'
CREATE INDEX [IX_FK_Task_Created_By]
ON [dbo].[Tasks]
    ([created_by]);
GO

-- Creating foreign key on [task_id] in table 'Task_Issue'
ALTER TABLE [dbo].[Task_Issue]
ADD CONSTRAINT [FK_Task_Issue_Task]
    FOREIGN KEY ([task_id])
    REFERENCES [dbo].[Tasks]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Issue_Task'
CREATE INDEX [IX_FK_Task_Issue_Task]
ON [dbo].[Task_Issue]
    ([task_id]);
GO

-- Creating foreign key on [task_id] in table 'Task_Message'
ALTER TABLE [dbo].[Task_Message]
ADD CONSTRAINT [FK_Task_Message_Task]
    FOREIGN KEY ([task_id])
    REFERENCES [dbo].[Tasks]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Message_Task'
CREATE INDEX [IX_FK_Task_Message_Task]
ON [dbo].[Task_Message]
    ([task_id]);
GO

-- Creating foreign key on [parent_task] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Task]
    FOREIGN KEY ([parent_task])
    REFERENCES [dbo].[Tasks]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Task'
CREATE INDEX [IX_FK_Task_Task]
ON [dbo].[Tasks]
    ([parent_task]);
GO

-- Creating foreign key on [task_type_id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Type]
    FOREIGN KEY ([task_type_id])
    REFERENCES [dbo].[Types]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Type'
CREATE INDEX [IX_FK_Task_Type]
ON [dbo].[Tasks]
    ([task_type_id]);
GO

-- Creating foreign key on [user_id] in table 'User_Roll'
ALTER TABLE [dbo].[User_Roll]
ADD CONSTRAINT [FK_User_Roll_User]
    FOREIGN KEY ([user_id])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Roll_User'
CREATE INDEX [IX_FK_User_Roll_User]
ON [dbo].[User_Roll]
    ([user_id]);
GO

-- Creating foreign key on [responsible] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_Task_Responsible1]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Task_Responsible1'
CREATE INDEX [IX_FK_Task_Responsible1]
ON [dbo].[Tasks]
    ([responsible]);
GO

-- Creating foreign key on [created_by] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Created_By]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Created_By'
CREATE INDEX [IX_FK_Issue_Created_By]
ON [dbo].[Issues]
    ([created_by]);
GO

-- Creating foreign key on [priority_id] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Priority]
    FOREIGN KEY ([priority_id])
    REFERENCES [dbo].[Priorities]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Priority'
CREATE INDEX [IX_FK_Issue_Priority]
ON [dbo].[Issues]
    ([priority_id]);
GO

-- Creating foreign key on [responsible] in table 'Issues'
ALTER TABLE [dbo].[Issues]
ADD CONSTRAINT [FK_Issue_Responsible]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Issue_Responsible'
CREATE INDEX [IX_FK_Issue_Responsible]
ON [dbo].[Issues]
    ([responsible]);
GO

-- Creating foreign key on [created_by] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_Created_By1]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Created_By1'
CREATE INDEX [IX_FK_Created_By1]
ON [dbo].[Projects]
    ([created_by]);
GO

-- Creating foreign key on [responsible] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_Responsible1]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Responsible1'
CREATE INDEX [IX_FK_Responsible1]
ON [dbo].[Projects]
    ([responsible]);
GO

-- Creating foreign key on [created_by] in table 'Subprojects'
ALTER TABLE [dbo].[Subprojects]
ADD CONSTRAINT [FK_Subproject_Created_By]
    FOREIGN KEY ([created_by])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Created_By'
CREATE INDEX [IX_FK_Subproject_Created_By]
ON [dbo].[Subprojects]
    ([created_by]);
GO

-- Creating foreign key on [responsible] in table 'Subprojects'
ALTER TABLE [dbo].[Subprojects]
ADD CONSTRAINT [FK_Subproject_Responsible]
    FOREIGN KEY ([responsible])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Subproject_Responsible'
CREATE INDEX [IX_FK_Subproject_Responsible]
ON [dbo].[Subprojects]
    ([responsible]);
GO

-- Creating foreign key on [project_id] in table 'User_Standard'
ALTER TABLE [dbo].[User_Standard]
ADD CONSTRAINT [FK_User_Standard_Project]
    FOREIGN KEY ([project_id])
    REFERENCES [dbo].[Projects]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Standard_Project'
CREATE INDEX [IX_FK_User_Standard_Project]
ON [dbo].[User_Standard]
    ([project_id]);
GO

-- Creating foreign key on [project_group_id] in table 'User_Standard'
ALTER TABLE [dbo].[User_Standard]
ADD CONSTRAINT [FK_User_Standard_Project_Group]
    FOREIGN KEY ([project_group_id])
    REFERENCES [dbo].[Project_Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Standard_Project_Group'
CREATE INDEX [IX_FK_User_Standard_Project_Group]
ON [dbo].[User_Standard]
    ([project_group_id]);
GO

-- Creating foreign key on [user_id] in table 'User_Standard'
ALTER TABLE [dbo].[User_Standard]
ADD CONSTRAINT [FK_User_Standard_User]
    FOREIGN KEY ([user_id])
    REFERENCES [dbo].[Users]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Standard_User'
CREATE INDEX [IX_FK_User_Standard_User]
ON [dbo].[User_Standard]
    ([user_id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------